package com.yichen.rent.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.yichen.rent.pojo.Resource;
import com.yichen.rent.response.HouseResourceResponse;
import com.yichen.rent.response.NewHouseResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yichen.rent.pojo.Newhouse;
import com.yichen.rent.service.NewhouseService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/newhouse")
public class NewhouseController {

	@Autowired
	private NewhouseService newhouseService;
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",convertNewHouseResourceList(newhouseService.findAll()));
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",convertNewtHouseResource(newhouseService.findById(id)));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Newhouse> pageList = newhouseService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<NewHouseResponse>(pageList.getTotalElements(), convertNewHouseResourceList(pageList.getContent()) ));
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",convertNewHouseResourceList(newhouseService.findSearch(searchMap)));
    }
	
	/**
	 * 增加
	 * @param newhouse
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Newhouse newhouse  ){
		newhouseService.add(newhouse);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param newhouse
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Newhouse newhouse, @PathVariable String id ){
		newhouse.setId(id);
		newhouseService.update(newhouse);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		newhouseService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	/**
	 * 获取房源总数
	 * @return
	 */
	@RequestMapping(value = "/count",method = RequestMethod.GET)
	public Result count() {
		return new Result(true,StatusCode.OK,"获取成功",newhouseService.getCount());
	}

	public NewHouseResponse convertNewtHouseResource(Newhouse newhouse){
		NewHouseResponse newHouseResponse = new NewHouseResponse();
		BeanUtils.copyProperties(newhouse,newHouseResponse,"image");
		if (newhouse.getImage()!=null && !newhouse.getImage().equals("")){
			List<String> strings = JSON.parseArray(newhouse.getImage(), String.class);
			newHouseResponse.setImage(strings);
		}else{
			newHouseResponse.setImage(new ArrayList<>());
		}
		return newHouseResponse;
	}

	public List<NewHouseResponse> convertNewHouseResourceList(List<Newhouse> list) {
		List<NewHouseResponse> newHouseResponses = new ArrayList<>();
		for(Newhouse resource : list) {
			NewHouseResponse houseResourceResponse = convertNewtHouseResource(resource);
			newHouseResponses.add(houseResourceResponse);
		}
		return newHouseResponses;
	}
}
